package fr.univ.lille1.server.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnore;

@Entity
@Table(name="USER")
public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6800029854593877711L;

	@Id
	@GeneratedValue
	private int id;

	@Column(unique=true, nullable=false)
	private String name;

	@Column(nullable=true)//For Google, and others
	private String password;

	@Column(unique=true, nullable=false)
	private String mail;

	public enum Status{
		ACTIVE, INACTIVE,
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable=false)
	private Status status;
	
	public enum Type{
		STANDARD, GOOGLE,
	}
	
	@Enumerated(EnumType.STRING)
	@Column(nullable=false)
	private Type userType;

	@ElementCollection
	@MapKeyColumn
	@Column(name="status", nullable=false, columnDefinition = "BIT")
	@CollectionTable(name="FRIENDS")
	private Map<User, Boolean> friends = new HashMap<User,Boolean>();

	public User() {
		super();
	}

	public User(String name, String password, String mail, Type userType) {
		super();
		this.name = name;
		this.password = password;
		this.mail = mail;
		this.status = Status.ACTIVE;
		this.userType = userType;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@JsonIgnore
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}
	
	public Type getType(){
		return userType;
	}
	
	public void setType(Type userType){
		this.userType=userType;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		User other = (User) obj;
		if (id != other.id){
			return false;
		}
		return true;
	}

	@JsonIgnore
	public List<User> getValidatedFriends() {
		List<User> friendsList = new ArrayList<User>(this.friends.keySet());
		for (User user : friendsList) {
			if(!this.friends.get(user)){
				friendsList.remove(user);
			}
		}
		return friendsList;
	}

	@JsonIgnore
	public List<User> getFriendsToValidate(){
		List<User> friendsList = new ArrayList<User>(this.friends.keySet());
		for (User user : friendsList) {
			if(this.friends.get(user)){
				friendsList.remove(user);
			}
		}
		return friendsList;
	}

	public void addFriend(User friend, Boolean status) {
		if(!this.friends.containsKey(friend)){
			this.friends.put(friend, status);
		}
	}

	public void changeFriendWith(User friend, Boolean status) {
		if(this.friends.containsKey(friend)){
			this.friends.put(friend, status);
		}
	}

	public boolean isFriendWith(User friend){
		if(this.friends.containsKey(friend)){
			return this.friends.get(friend);
		}
		return false;
	}



}
